* Make Appendix Table A3 (out-of-hours results)

* First set up the dataset

*1) Create the connected set for 2009 onwards
do "$dofile/8fi. create_09onwards_set.do"

*2) Use set to create the 2009 onwards dataset - uses the previous dofile and produces masterdata_09onwards.dta, which is used below for the rest of the analysis
do "$dofile/8fii. create_09onwards_data.do"

* Now do analysis - first column 1 (All hours) and col 2 (out of hours) afterwards
use "$savedata/masterdata_09onwards.dta", clear

drop if exittime==.
egen vol_trust = sum(one), by(pconsult trust_num)

foreach x in 10 25 50 100{
egen vol`x' = sum(one) if akm_set_mainspef`x'==1, by(pconsult)	
gen sample`x'=0
replace sample`x'=1 if akm_set_mainspef`x'==1 & vol_trust>=`x'
}

keep if sample25==1
gen vol = vol25

preserve

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step

su docfe
matrix observe_std = r(sd)
su adj_docfe
matrix observe_adjstd = r(sd)

su docfe
matrix observe_var = r(Var)
su adj_docfe
matrix observe_adjvar = r(Var)

su docfe, det
matrix observe_p10 = r(p10)
su adj_docfe, det
matrix observe_adjp10 = r(p10)

su docfe, det
matrix observe_p25 = r(p25)
su adj_docfe, det
matrix observe_adjp25 = r(p25)

su docfe, det
matrix observe_p50 = r(p50)
su adj_docfe, det
matrix observe_adjp50 = r(p50)

su docfe, det
matrix observe_p75 = r(p75)
su adj_docfe, det
matrix observe_adjp75 = r(p75)

su docfe, det
matrix observe_p90 = r(p90)
su adj_docfe, det
matrix observe_adjp90 = r(p90)

restore

capture program drop myboot2

program define myboot2, rclass

* draw my bootstrap sample
preserve
bsample, strata(doctor_id)

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step

su docfe
return scalar d_std = r(sd)
su adj_docfe
return scalar d_adjstd = r(sd)

su docfe
return scalar d_var = r(Var)
su adj_docfe
return scalar d_adjvar = r(Var)

su docfe, det
return scalar d_p10 = r(p10)
su adj_docfe, det
return scalar d_adjp10 = r(p10)

su docfe, det
return scalar d_p25 = r(p25)
su adj_docfe, det
return scalar d_adjp25 = r(p25)

su docfe, det
return scalar d_p50 = r(p50)
su adj_docfe, det
return scalar d_adjp50 = r(p50)

su docfe, det
return scalar d_p75 = r(p75)
su adj_docfe, det
return scalar d_adjp75 = r(p75)

su docfe, det
return scalar d_p90 = r(p90)
su adj_docfe, det
return scalar d_adjp90 = r(p90)

restore

end


** Then run bootstrap here

#delimit ;
* Simulate 199 times to test;
simulate diff_std = r(d_std) diff_adjstd = r(d_adjstd) diff_var = r(d_var) diff_adjvar = r(d_adjvar)
diff_p10 = r(d_p10) diff_adjp10 = r(d_adjp10) diff_p25 = r(d_p25) diff_adjp25 = r(d_adjp25) diff_p50 = r(d_p50) diff_adjp50 = r(d_adjp50)
diff_p75 = r(d_p75) diff_adjp75 = r(d_adjp75) diff_p90 = r(d_p90) diff_adjp90 = r(d_adjp90)
, reps(199) seed(32786105): myboot2;


#delimit ;
* Boostrap;
bstat, stat(observe_std, observe_adjstd, observe_var, observe_adjvar,
observe_p10, observe_adjp10, observe_p25, observe_adjp25, observe_p50, observe_adjp50,
observe_p75, observe_adjp75, observe_p90, observe_adjp90);

#delimit cr
* Output results
putexcel set "$results/TableA3_col1.xlsx", replace
putexcel A1 = "Variable"
putexcel B1 = "Coefficient"
putexcel C1 = "Std. error"

matrix b = e(b)'
putexcel A2 = matrix(b), rownames

matrix se = e(se)'
putexcel C2 = matrix(se)


** Now repeat for out-of-hours only (column 2)

use "$savedata/masterdata_09onwards.dta", clear

* Keep only out-of-hours patients
drop if exittime==.
keep if dow==0 | dow==6 | exittime<800 | exittime>2000

egen vol_trust = sum(one), by(pconsult trust_num)

* Doctor FEs

foreach x in 10 25 50 100{
egen vol`x' = sum(one) if akm_set_mainspef`x'==1, by(pconsult)	
gen sample`x'=0
replace sample`x'=1 if akm_set_mainspef`x'==1 & vol_trust>=`x'
}
*/

keep if sample25==1
gen vol = vol25

preserve

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step

su docfe
matrix observe_std = r(sd)
su adj_docfe
matrix observe_adjstd = r(sd)

su docfe
matrix observe_var = r(Var)
su adj_docfe
matrix observe_adjvar = r(Var)

su docfe, det
matrix observe_p10 = r(p10)
su adj_docfe, det
matrix observe_adjp10 = r(p10)

su docfe, det
matrix observe_p25 = r(p25)
su adj_docfe, det
matrix observe_adjp25 = r(p25)

su docfe, det
matrix observe_p50 = r(p50)
su adj_docfe, det
matrix observe_adjp50 = r(p50)

su docfe, det
matrix observe_p75 = r(p75)
su adj_docfe, det
matrix observe_adjp75 = r(p75)

su docfe, det
matrix observe_p90 = r(p90)
su adj_docfe, det
matrix observe_adjp90 = r(p90)

restore

capture program drop myboot2

program define myboot2, rclass

* draw my bootstrap sample
preserve
bsample, strata(doctor_id)

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

collapse (mean) docfe residual sigma* vol, by(doctor_id)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe = docfe*signal_2step

su docfe
return scalar d_std = r(sd)
su adj_docfe
return scalar d_adjstd = r(sd)

su docfe
return scalar d_var = r(Var)
su adj_docfe
return scalar d_adjvar = r(Var)

su docfe, det
return scalar d_p10 = r(p10)
su adj_docfe, det
return scalar d_adjp10 = r(p10)

su docfe, det
return scalar d_p25 = r(p25)
su adj_docfe, det
return scalar d_adjp25 = r(p25)

su docfe, det
return scalar d_p50 = r(p50)
su adj_docfe, det
return scalar d_adjp50 = r(p50)

su docfe, det
return scalar d_p75 = r(p75)
su adj_docfe, det
return scalar d_adjp75 = r(p75)

su docfe, det
return scalar d_p90 = r(p90)
su adj_docfe, det
return scalar d_adjp90 = r(p90)

restore

end


** Then run bootstrap here

#delimit ;
* Simulate 199 times to test;
simulate diff_std = r(d_std) diff_adjstd = r(d_adjstd) diff_var = r(d_var) diff_adjvar = r(d_adjvar)
diff_p10 = r(d_p10) diff_adjp10 = r(d_adjp10) diff_p25 = r(d_p25) diff_adjp25 = r(d_adjp25) diff_p50 = r(d_p50) diff_adjp50 = r(d_adjp50)
diff_p75 = r(d_p75) diff_adjp75 = r(d_adjp75) diff_p90 = r(d_p90) diff_adjp90 = r(d_adjp90)
, reps(199) seed(32786105): myboot2;


#delimit ;
* Boostrap;
bstat, stat(observe_std, observe_adjstd, observe_var, observe_adjvar,
observe_p10, observe_adjp10, observe_p25, observe_adjp25, observe_p50, observe_adjp50,
observe_p75, observe_adjp75, observe_p90, observe_adjp90);

#delimit cr
* Output results
putexcel set "$results/TableA3_col2.xlsx", replace
putexcel A1 = "Variable"
putexcel B1 = "Coefficient"
putexcel C1 = "Std. error"

matrix b = e(b)'
putexcel A2 = matrix(b), rownames

matrix se = e(se)'
putexcel C2 = matrix(se)

